/**
 * Copyright (C) 2009-2012, KingHool.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses.
 */
package com.kinghool.smodel.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.List;

import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * 
 * @Author Allen Deng(dengtailin@gmail.com) 
 */
public abstract class JDomUtils {
	
	
	@SuppressWarnings("unchecked")
	public static int afterLastIndexOf(Element parent, String childName) {
        List<Element> children = parent.getChildren(childName, parent.getNamespace());
        if (children.size() > 0) {
            return parent.indexOf(children.get(children.size()-1)) + 1;
        }
        return -1;
    }
    public static int indexOf(Element parent, String childName) {
        Element child = parent.getChild(childName, parent.getNamespace());
        if (child != null) {
            return parent.indexOf(child);
        }
        return -1;
    } 
 
    public static int getInsertPosition(Element parent, String[] beforeTags, String[] afterTags) {
        int index = -1;
        for (int i = 0; i < beforeTags.length && index == -1; i++) {
            index = JDomUtils.afterLastIndexOf(parent, beforeTags[i]);
        }
        if (index == -1) {                              
            for (int i = 0; i < afterTags.length && index == -1; i++) {
                index = JDomUtils.indexOf(parent, afterTags[i]);
            }
        }
        return index;
    }
    public static void insertChild(Element parent, Element child, String[] beforeTags, String[] afterTags) {
        int index = JDomUtils.getInsertPosition(parent, beforeTags, afterTags);
        if (index != -1) {
            parent.addContent(index, child);
        } else {
            parent.addContent(child);
        }
    }
    
    public static void insertChild(Element parent, Content child, int index) {
    	if (index < 0 || index >= parent.getContentSize()) {
    		parent.addContent(child);
    	} else {
    		parent.addContent(index, child);
    	}
    }
    
    public static void insertChild(Element parent, Element child, String[] allChildElementNames) {
        String[] beforeTags = new String[0];
        String[] afterTags = new String[0];
        String childElementName = child.getName();
        int pos = -1;
        for (int i = 0; i < allChildElementNames.length; i++) {
            if (childElementName.equals(allChildElementNames[i])) {
                pos = i;
                break;
            }
        }
        if (pos != -1) {
            beforeTags = new String[pos + 1];
            afterTags = new String[allChildElementNames.length - pos - 1];
            for (int i = 0; i < pos + 1; i++) {
                beforeTags[i] = allChildElementNames[pos - i];
            }            
            System.arraycopy(allChildElementNames, pos + 1, afterTags, 0, afterTags.length);
        }
        insertChild(parent, child, beforeTags, afterTags);
    }
  
    public static void writeXml(Document doc, OutputStream outStream) throws IOException {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(doc, outStream);
        outStream.flush();
        outStream.close();
    }
    
    public static void writeXml(Document doc, Writer writer) throws IOException {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(doc, writer);
        writer.flush();
        writer.close();
    } 
}
